﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer.Properties;
using System.Transactions;

namespace DataAccessLayer
{
    public partial class SqlDataProvider : IDataProvider
    {
        public void DeleteAdmCharacteristicRelations(DTO.AdmCharacteristic c)
        {
            using (IMModel.Domain.IMDataDataContext db = new IMModel.Domain.IMDataDataContext(Settings.Default.IndexMarkConnectionString))
            {
                using (var transaction = new TransactionScope())
                {
                    db.CharacteristicResponsibles.DeleteAllOnSubmit(db.CharacteristicResponsibles.Where(f => f.IDCharacteristicTerm == c.IDCharacteristicTerm));
                    db.CharacteristicGroups.DeleteAllOnSubmit(db.CharacteristicGroups.Where(f => f.IDCharacteristicTerm == c.IDCharacteristicTerm));
                    db.CharacteristicTerms.DeleteAllOnSubmit(db.CharacteristicTerms.Where(w => w.IDCharacteristicTerm == c.IDCharacteristicTerm));
                    db.SubmitChanges();

                    transaction.Complete();
                }
            }
        }

        public void DeleteAdmSectionRelations(DTO.AdmSection s)
        {
            using (IMModel.Domain.IMDataDataContext db = new IMModel.Domain.IMDataDataContext(Settings.Default.IndexMarkConnectionString))
            {
                using (var transaction = new TransactionScope())
                {
                    //db.CharacteristicTerms.DeleteAllOnSubmit(db.CharacteristicTerms.Where(w => w.IDSectionTerm == s.IDSectionTerm));
                    db.CategorySectionTerms.DeleteAllOnSubmit(db.CategorySectionTerms.Where(w => w.IDSectionTerm == s.IDSectionTerm));
                    db.SectionTerms.DeleteAllOnSubmit(db.SectionTerms.Where(w => w.IDSectionTerm == s.IDSectionTerm));
                    db.SubmitChanges();

                    transaction.Complete();
                }
            }
        }

        public void DeleteAdmDepartmentRelations(DTO.AdmDepartment d)
        {
            using (IMModel.Domain.IMDataDataContext db = new IMModel.Domain.IMDataDataContext(Settings.Default.IndexMarkConnectionString))
            {
                using (var transaction = new TransactionScope())
                {
                    db.Respondents.DeleteAllOnSubmit(db.Respondents.Where(w => w.IDDepartmentTerm == d.IDDepartmentTerm));
                    db.DepartmentTerms.DeleteAllOnSubmit(db.DepartmentTerms.Where(w => w.IDDepartmentTerm == d.IDDepartmentTerm));
                    db.SubmitChanges();

                    transaction.Complete();
                }
            }
        }

        public void ClearAnswerLog()
        {
            using (IMModel.Domain.IMDataDataContext db = new IMModel.Domain.IMDataDataContext(Settings.Default.IndexMarkConnectionString))
            {
                var q = from a in db.Answers select a;
                db.Answers.DeleteAllOnSubmit(q);
                db.SubmitChanges();
            }
        }

        public void ClearInputLog()
        {
            using (IMModel.Domain.IMDataDataContext db = new IMModel.Domain.IMDataDataContext(Settings.Default.IndexMarkConnectionString))
            {
                var q = from a in db.InputLogs select a;
                db.InputLogs.DeleteAllOnSubmit(q);
                db.SubmitChanges();
            }
        }

        public void ClearUniversityStruct(int IDTerm)
        {
            using (IMModel.Domain.IMDataDataContext db = new IMModel.Domain.IMDataDataContext(Settings.Default.IndexMarkConnectionString))
            {
                using (var transaction = new TransactionScope())
                {
                    // Answer + GradationAnswer
                    foreach (IMModel.Domain.Answer ans in db.Answers.Where(x => x.Respondent.DepartmentTerm.IDTerm == IDTerm
                        && x.IDOldAnswer == null))
                    {
                        db.DeletingAnswer(ans.IDAnswer);
                    }

                    // Respondent
                    db.Respondents.DeleteAllOnSubmit(
                        db.Respondents.Where(x => x.DepartmentTerm.IDTerm == IDTerm));
                    db.SubmitChanges();

                    // DepartmentTerm
                    foreach(IMModel.Domain.DepartmentTerm dt in db.DepartmentTerms.Where(x => x.IDTerm == IDTerm
                        && x.IDParentDepartmentTerm == null))
                    {
                        db.DeletingDepartmentTerm(dt.IDDepartmentTerm);
                    }

                    transaction.Complete();
                }
            }
        }

        public void ClearIndexMarkStruct(int IDTerm)
        {
            using (IMModel.Domain.IMDataDataContext db = new IMModel.Domain.IMDataDataContext(Settings.Default.IndexMarkConnectionString))
            {
                using (var transaction = new TransactionScope())
                {
                    // Answer + GradationAnswer
                    foreach (IMModel.Domain.Answer ans in db.Answers.Where(x => x.CharacteristicTerm.IDTerm == IDTerm
                        && x.IDOldAnswer == null))
                    {
                        db.DeletingAnswer(ans.IDAnswer);
                    }

                    // CharacteristicGroups
                    db.CharacteristicGroups.DeleteAllOnSubmit(
                        db.CharacteristicGroups.Where(x => x.CharacteristicTerm.IDTerm == IDTerm));
                    db.SubmitChanges();

                    // CharacteristicResponsibles
                    db.CharacteristicResponsibles.DeleteAllOnSubmit(
                        db.CharacteristicResponsibles.Where(x => x.CharacteristicTerm.IDTerm == IDTerm));
                    db.SubmitChanges();

                    // CharacteristicTerm
                    foreach (IMModel.Domain.CharacteristicTerm ct in db.CharacteristicTerms.Where(x => x.IDTerm == IDTerm))
                    {
                        db.DeletingCharacteristicTerm(ct.IDCharacteristicTerm);
                    }

                    // CategorySectionTerms
                    db.CategorySectionTerms.DeleteAllOnSubmit(
                        db.CategorySectionTerms.Where(x => x.CategoryTerm.IDTerm == IDTerm ||
                            x.SectionTerm.IDTerm == IDTerm));
                    db.SubmitChanges();

                    // CategoryTerms
                    db.CategoryTerms.DeleteAllOnSubmit(
                        db.CategoryTerms.Where(x => x.IDTerm == IDTerm));
                    db.SubmitChanges();

                    // SectionTerm
                    foreach (IMModel.Domain.SectionTerm st in db.SectionTerms.Where(x => x.IDTerm == IDTerm
                        && x.IDParentSectionTerm == null))
                    {
                        db.DeletingSectionTerm(st.IDSectionTerm);
                    }

                    // GradationTerm
                    foreach (IMModel.Domain.GradationTerm gt in db.GradationTerms.Where(x => x.IDTerm == IDTerm
                        && x.IDParentGradationTerm == null))
                    {
                        db.DeletingGradationTerm(gt.IDGradationTerm);
                    }

                    transaction.Complete();
                }
            }
        }
    }
}
